from django.http import JsonResponse

from .models import Covid, forcast_lightgbm, forcast_rf, forcast_rnn, forcast_xgboost, forcast_pr

from .forcast import forcast_model
import datetime


def query(request):
    try:
        model = request.POST.get('model')
        area = request.POST.get('area', None)
        country = request.POST.get('country')
        area1 = country + '/' + area
        date1 = request.POST.get('datestart')
        date2 = request.POST.get('dateend')
        # print(model, area, country, date1, date2)

        datestart = datetime.datetime.strptime(date1, '%Y-%m-%d')
        dateend = datetime.datetime.strptime(date2, '%Y-%m-%d')

        datelist = []
        while datestart < dateend:
            datestart += datetime.timedelta(days=1)
            datelist.append(datestart.strftime('%Y-%m-%d'))

        retlist = []
        if model == 'lightgbm':
            try:
                for date in datelist:
                    info = Covid.objects.using('predict').filter(area=area,
                                                                 country=country,
                                                                 date=date, )
                    dict1 = forcast_model.get_forcast(area1, date)[0]
                    for i in info:
                        retlist.append({
                            # 'area':i.area,
                            # 'country':i.country,
                            'date': i.date,
                            'confirmedCases': i.confirmedCases,
                            'fatalities': i.fatalities,
                            'predConfirmedCases': dict1.get('predConfirmedCases'),
                            'predFatalities': dict1.get('predFatalities'),
                        })
            except Exception as ex:
                print(ex)
        else:
            for date in datelist:
                info = Covid.objects.using('predict').filter(area=area,
                                                             country=country,
                                                             date=date, ).first()

                if model == '多项式':
                    modeluse = forcast_pr
                    info1 = modeluse.objects.using('predict').filter(area=area1,
                                                                     country=country,
                                                                     date=date, ).first()
                elif model == '随机森林':
                    modeluse = forcast_rf
                    info1 = modeluse.objects.using('predict').filter(area=country,
                                                                     country=area1,
                                                                     date=date, ).first()
                elif model == '深度学习':
                    modeluse = forcast_rnn
                    info1 = modeluse.objects.using('predict').filter(area=area,
                                                                     country=country,
                                                                     date=date, ).first()
                else:
                    modeluse = forcast_xgboost
                    info1 = modeluse.objects.using('predict').filter(area=area1,
                                                                     country=country,
                                                                     date=date, ).first()
                retlist.append({
                    'date': date,
                    'confirmedCases': info.confirmedCases,
                    'fatalities': info.fatalities,
                    'predConfirmedCases': info1.predConfirmedCases,
                    'predFatalities': info1.predFatalities,
                })
        return JsonResponse({'ret': 0, 'retlist': retlist}, safe=False)
    except Covid.DoesNotExist:
        return JsonResponse({'ret': 1, 'message': 'Not found'})


def queryall(request):
    qs = Covid.objects.using('predict').values()
    retlist = list(qs)
    return JsonResponse({'ret': 0, 'retlist': retlist})


def predict_lightgbm(request):
    place = request.GET.get('country')
    date1 = request.GET.get('datestart')
    date2 = request.GET.get('dateend')

    datestart = datetime.datetime.strptime(date1, '%Y-%m-%d')
    dateend = datetime.datetime.strptime(date2, '%Y-%m-%d')

    datelist = []
    while datestart < dateend:
        datestart += datetime.timedelta(days=1)
        datelist.append(datestart.strftime('%Y-%m-%d'))

    retlist = []
    for date in datelist:
        retlist.extend(forcast_model.get_forcast(place, date))
    return JsonResponse({'ret': 0, 'retlist': retlist}, safe=False)


def predict_pr(request):
    try:
        area1 = request.GET.get('area', None)
        country = request.GET.get('country')
        area = country + '/' + area1
        date1 = request.GET.get('datestart')
        date2 = request.GET.get('dateend')

        datestart = datetime.datetime.strptime(date1, '%Y-%m-%d')
        dateend = datetime.datetime.strptime(date2, '%Y-%m-%d')

        datelist = []
        while datestart < dateend:
            datestart += datetime.timedelta(days=1)
            datelist.append(datestart.strftime('%Y-%m-%d'))

        retlist = []
        for date in datelist:
            info = forcast_pr.objects.using('predict').filter(area=area,
                                             country=country,
                                             date=date, )

            for i in info:
                retlist.append({
                    # 'area': i.area,
                    # 'country': i.country,
                    'date': i.date,
                    'predConfirmedCases': i.predConfirmedCases,
                    'predFatalities': i.predFatalities,
                })
        return JsonResponse({'ret': 0, 'retlist': retlist}, safe=False)
    except forcast_pr.DoesNotExist:
        return JsonResponse({'ret': 1, 'message': 'Not found'})


def predict_rf(request):
    try:
        area = request.GET.get('area', None)
        country = request.GET.get('country')
        date = request.GET.get('date')
        # 数据库录入的时候出了些小问题,搞反了area和country,问题不大
        info = forcast_rf.objects.using('predict').filter(country=area,
                                         area=country,
                                         date=date, )
        retlist = []
        for i in info:
            retlist.append({
                'area': i.country,
                'country': i.area,
                'date': i.date,
                'predConfirmedCases': i.predConfirmedCases,
                'predFatalities': i.predFatalities,
            })
        return JsonResponse({'ret': 0, 'retlist': retlist}, safe=False)
    except forcast_rf.DoesNotExist:
        return JsonResponse({'ret': 1, 'message': 'Not found'})


def predict_rnn(request):
    try:
        area = request.GET.get('area', None)
        country = request.GET.get('country')
        date = request.GET.get('date')
        info = forcast_rnn.objects.using('predict').filter(area=area,
                                          country=country,
                                          date=date, )
        retlist = []
        for i in info:
            retlist.append({
                'area': i.area,
                'country': i.country,
                'date': i.date,
                'predConfirmedCases': i.predConfirmedCases,
                'predFatalities': i.predFatalities,
            })
        return JsonResponse({'ret': 0, 'retlist': retlist}, safe=False)
    except forcast_rnn.DoesNotExist:
        return JsonResponse({'ret': 1, 'message': 'Not found'})


def predict_xgb(request):
    try:
        area = request.GET.get('area', None)
        country = request.GET.get('country')
        date = request.GET.get('date')
        info = forcast_xgboost.objects.using('predict').filter(area=area,
                                              country=country,
                                              date=date, )
        retlist = []
        for i in info:
            retlist.append({
                'area': i.area,
                'country': i.country,
                'date': i.date,
                'predConfirmedCases': i.predConfirmedCases,
                'predFatalities': i.predFatalities,
            })
        return JsonResponse({'ret': 0, 'retlist': retlist}, safe=False)
    except forcast_xgboost.DoesNotExist:
        return JsonResponse({'ret': 1, 'message': 'Not found'})
